package com.lori.android.core.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射工具类
 * 处理方法、字段等的反射
 *
 * @author lori.lin
 * @version 1.0
 * @date 14-8-26.
 */
public class ReflectUtil {

    /**
     * 方法的实现
     * @param className 类名
     * @param methodName 方法名
     * @param properties 参数类型
     * @param values 参数值
     * @return 返回值
     */
    public static Object invokeMethod(String className,String methodName,Class[] properties,Object[] values)
    {
        try {
            Class<?> c = Class.forName(className);
            Method method = c.getMethod(methodName,properties);
            return method.invoke(c,values);
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 反射属性
     * @param object 类名
     * @param fieldName 类中包含的属性名
     * @return 返回值
     */
    public static Object getObject(Object object,String fieldName)
    {
        Object result = null;
        try {
            Field field = object.getClass().getField(fieldName);
            field.setAccessible(true);
            result = field.get(object);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 带有固定参数的构造函数
     * @param c
     * @param objects
     * @return
     */
    public static Object getConstructorInstance(Class c,Object[] objects)
    {
        Object result = null;
        try {
            Constructor constructor = c.getDeclaredConstructor(objects.getClass());
            result = constructor.newInstance(objects);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 获取object类中所有的属性(包括private)
     * @param object
     * @return String[]
     */
    public static String[] getFields(Object object)
    {
        Field[] fields = object.getClass().getDeclaredFields();
        String[] results = new String[fields.length];
        for(int i =0;i<results.length;i++)
        {
            results[i] = fields[i].getName();
        }
        return results;
    }

    /**
     * 设置字段的值（包括私有）
     * @param object
     * @param key
     * @param value
     */
    public static void setFieldValue(Object object,String key,Object value)
    {
        Field field = null;
        try {
            field = object.getClass().getDeclaredField(key);
            field.setAccessible(true);
            field.set(object,value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();

        }

    }

    /**
     * 获取对象的值
     * @param object
     * @param key
     * @return
     */
    public static Object getFieldValue(Object object,String key)
    {
        Object result = null;
        try {
            Field field = object.getClass().getDeclaredField(key);
            field.setAccessible(true);
            result = field.get(object);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }


}
